package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CmnProductionListActivity;
import com.hemaapp.yjnh.bean.BlogType;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：农需中的商品分类ViewPager适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/7/29 11:17
 * 修改备注：
 */
public class AgriculturalSortAdapter extends PagerAdapter {

    private Context mContext;
    ArrayList<ArrayList<BlogType>> types = new ArrayList<>();
    private int page = 1;

    public AgriculturalSortAdapter(Context context, ArrayList<BlogType> types) {
        this.mContext = context;
//        page = (types.size()-1) / 8 + 1;
//        int lastSize = types.size()%8;
//        for (int i = 0;i < page-1;i++) {
//            ArrayList<BlogType> group = new ArrayList<>();
//            for (int j = i*8; j<(i+1)*8;j++)
//                group.add(types.get(j));
//            this.types.add(group);
//        }
//        ArrayList<BlogType> group = new ArrayList<>();
//        for (int j = (page-1)*8; j<lastSize+(page-1)*8;j++)
//            group.add(types.get(j));
//        this.types.add(group);
        //先计算出满页的数据
        page = types.size()/8;
        for (int i = 0;i < page;i++) {
            ArrayList<BlogType> group = new ArrayList<>();
            for (int j = i*8; j<(i+1)*8;j++)
                group.add(types.get(j));
            this.types.add(group);
        }
        //在计算非满页的情况
        int lastSize = types.size()%8;
        if (lastSize > 0) {
            ArrayList<BlogType> group = new ArrayList<>();
            for (int j = 0; j<lastSize;j++)
                group.add(types.get(j+page*8));
            this.types.add(group);
            page++;
        }
    }

    @Override
    public int getCount() {
        return page;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View mView = LayoutInflater.from(mContext).inflate(
                R.layout.viewpager_agricultural_sort, null);
        RecyclerView recyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 4);
        recyclerView.setLayoutManager(layoutManager);
        SortAdapter adapter = new SortAdapter(types.get(position));
        recyclerView.setAdapter(adapter);
        container.addView(mView);
        return mView;
    }

    public class SortAdapter extends RecyclerView.Adapter<SortAdapter.ViewHolder> {

        private ArrayList<BlogType> datas;

        public SortAdapter(ArrayList<BlogType> datas) {
            this.datas = datas;
        }

        @Override
        public SortAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_agricultural_sort, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(SortAdapter.ViewHolder holder, int position) {
            BlogType data = datas.get(position);
            holder.setData(data);
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            @Bind(R.id.iv_sort)
            ImageView ivSort;
            @Bind(R.id.tv_sort)
            TextView tvSort;

            BlogType data;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (data != null) {
                            Intent it = new Intent(mContext, CmnProductionListActivity.class);
                            it.putExtra("title", data.getName());
                            it.putExtra("keytype", "17");
                            if (data.getName().equals("全部"))
                                it.putExtra("keyid", "无");
                            else
                                it.putExtra("keyid", data.getName());
                            it.putExtra("keyword", "");
                            it.putExtra("saleflag", "");
                            it.putExtra("topflag", "");
                            it.putExtra("cheapflag", "");
                            it.putExtra("score_percent", "");
                            it.putExtra("seller_id", "");
                            it.putExtra("district_name", "");
                            mContext.startActivity(it);
                        }
                    }
                });
            }

            public void setData(BlogType data) {
                this.data = data;
                tvSort.setText(data.getName());
                ImageUtils.loadAssort(mContext, data.getImgurl(), ivSort);
//                Glide.with(mContext).load(data.getImgurl())
//                        .placeholder(R.mipmap.all_sort).error(R.mipmap.all_sort)
//                        .centerCrop().crossFade().into(ivSort);
            }
        }
    }
}
